/**
 * Created with JetBrains PhpStorm.
 * User: Murat Paksoy
 * Date: 06.10.2013
 * Time: 22:31
 */

jQuery.fx.interval = 10;
var sliderTime = 0;
var sliderTimeTN = 0;
// Menü sabitleme fonksiyonu
var menu; // Menü Seçici
var menuStart; // Menünün başlangıç noktası
// ListNews Load
var ListNewsBottom;
var kategoriId = 0;
var sondakKeyword = '';
var dateNow = new Date();
var userLastId = 0;
var newsLastId = 0;
var cityCode = 34;
var fixedMenuKont = false;
var checkPoint;

$(document).ready(function () {
    // Lazy Load Start
    //    $(".lazyload").lazyload({ effect: "fadeIn", threshold: 215 });

    $('a[rel*=external]').on('click', function () {
        window.open($(this).attr('href'));
        return false;
    });

    if ($('.wc-one').length > 0) {
        $('.wc-one .sld').slides({
            container: 'containers',
            preload: false,
            effect: 'slide, fade',
            crossfade: false,
            slideSpeed: 250,
            fadeSpeed: 200,
            play: 7000,
            pause: 2500,
            hoverPause: true,
            generateNextPrev: false,
            next: 'nxt',
            prev: 'prv',
            generatePagination: true
        });
        sliderTN();
    }
    // Menü Sabitleme
    menu = $('#Menu');
    if (menu.html()) {
        menuStart = $(menu).offset().top;
        $.event.add(window, "scroll", menuFixed);
    }

    $('.LogoSmall').click(function () {
        $('body,html').animate({
            scrollTop: $('#ListNewNews').position().top - 170
        }, 400);
        $('.LogoSmall > span').slideUp();
        return false;
    });

    $('#MultiMedia > .Tabs > li').click(function () {
        i = $(this).index();

        $('#MultiMedia > .Tabs > li').removeClass('Click').eq(i).addClass('Click');
        startAjax('#MultiMedia > .Content', true);
        $('#MultiMedia > .Content > .Items').load('/ajax/multiMedyaKutu.aspx?type=' + i, function () {
            endAjax();
        });
    });

    startAjax('#Weather', true);
    $.getJSON('/ajax/ajaxWeather.aspx?cityCode=34', function (data) {
        $('#Weather > img').attr('src', data.imgSrc);
        $('#Weather > p > span').html(data.weatherValue);
        $('#Weather > p > strong').text(data.city);
        endAjax();
    });

    $('#Weather > p > strong').toggle(function () {
        $('#Weather > ul').show();
    }, function () {
        $('#Weather > ul').hide();
    });

    $('#Weather > ul > li').click(function () {
        cityCode = $(this).attr('data-text');
        startAjax('#Weather', true);
        $.getJSON('http://www.f5haber.com/ajax/ajaxWeather.aspx?cityCode=' + cityCode, function (data) {
            $('#Weather > img').attr('src', data.imgSrc);
            $('#Weather > p > span').html(data.weatherValue);
            $('#Weather > p > strong').text(data.city);
            $('#Weather > p > strong').click();
            endAjax();
        });
    });



});

var ListNewsOffset = 1;
var ListNewsLoad = 0;
var ListNewsTimer;

function MenuDegis(id) {
    $('#' + id).addClass("Click");
}

function LoadNewNews() {
    $.get('/ajax/ajaxListNews.aspx?way=1&cat=' + kategoriId + '&offset=1&lastId=' + newsLastId, function (data) {
        if (data != "") $('#ListNewNews > li > a').removeClass('HotNews');
        $('#ListNewNews').prepend(data);
        $('#ListNewNews > li').hover(function () {
            i = $(this).index();
            $('#ListNewNews > li > a').eq(i).removeClass('HotNews');
            $('.LogoSmall > span').slideUp();
        });
    });
}

function LoadNewAjans() {
    $.get('/ajax/ajaxListAjans.aspx?way=1&key=' + sondakKeyword + '&offset=1&lastId=' + newsLastId, function (data) {
        if (data != "") $('#ListNewNews > li > a').removeClass('HotNews');
        $('#ListNewNews').prepend(data);
        $('#ListNewNews > li').hover(function () {
            i = $(this).index();
            $('#ListNewNews > li > a').eq(i).removeClass('HotNews');
            $('.LogoSmall > span').slideUp();
        });
    });
}

function NewNewsAlert(yeniHaberSayi) {
    $('.LogoSmall > span').html(yeniHaberSayi + " yeni");
    $('.LogoSmall > span').slideDown();
}

function ListNewsManuel() {
    ListNewsOffset += 1;
    //alert(ListNewsOffset);
        ListNewsLoad = 1;
        startAjax('#ListNewsLoad', true);
        $.get('/ajax/ajaxListNews.aspx?way=0&cat=' + kategoriId + '&offset=' + ListNewsOffset + '&lastId=' + userLastId, function (data) {
            $('#ListNews').append(data);
            data = null;
            ListNewsLoad = 0;
            endAjax();
        });

        if (ListNewsOffset > 1 && !fixedMenuKont) {
            fixedMenuKont = true;
            $('ul.Links').addClass('bottomFix');
        }
}

function ListNews() {
    var scrollTop = $(window).scrollTop();
    var s = ($(document).height() - $(window).height()) - 900;
    var st = ($(document).height() - $(window).height()) + 200;
    if(scrollTop > s && scrollTop <= st && ListNewsLoad == 0)
    {
        ListNewsManuel();
    }
}

function ListAjansManuel() {
	ListNewsOffset += 1;
	ListNewsLoad = 1;
	startAjax('#ListNewsLoad', true);
	$.get('/ajax/ajaxListAjans.aspx?way=0&key=' + sondakKeyword + '&offset=' + ListNewsOffset + '&lastId=' + userLastId, function (data) {
		$('#ListNews').append(data);
		data = null;
		ListNewsLoad = 0;
		endAjax();
	});
}

function ListAjans() {
    var scrollTop = $(window).scrollTop();
    var s = ($(document).height() - $(window).height()) - 200;
    var st = ($(document).height() - $(window).height()) + 200;
    if(scrollTop > s && scrollTop <= st && ListNewsLoad == 0)
    {
        ListAjansManuel();
    }
}

function checkAlertSpan() {
    var p = $(window).scrollTop();
    if (p < checkPoint) {
        $('.LogoSmall > span').slideUp();
    }
}

function menuFixed()
{
    var p = $(window).scrollTop();
    $(menu).css('position',((p) > menuStart) ? 'fixed' : 'relative');
    $(menu).css('z-index',((p) > menuStart) ? '7000' : '');
    $(menu).css('top',((p) > menuStart) ? '0px' : '');
    if (p > menuStart)
        $('.LogoSmall').slideDown('slow');
    else {
        $('.LogoSmall').hide();
        fixedMenuKont = false;
        $('ul.Links').removeClass('bottomFix');
    }
}

var currentItem = 0;
var imageCount = 0;
var hlTimer;
function slider()
{
    $('#Headline > ul > li > a:first').addClass('Hover');
    $('#Headline > .Pag > ul > li').mouseover(function () {
        currentItem = $(this).index();
        if (currentItem < 20) {
            $('#Headline > .Pag > ul > li > a').removeClass('Hover');
            $(this).children('a').addClass('Hover');
            $('#Headline > .Items > li').hide();
            $('#Headline > .Items > li').eq(currentItem).show();
        }
        clearInterval(hlTimer);
    }).mouseout(function () {
        hlTimer = setInterval("headlineTrigger()", 5000);
    });
    hlTimer = setInterval("headlineTrigger()", 5000);
}

function headlineTimerMouseOver (i)
{
    $('#Headline > .Items > li').fadeOut();
    $('#Headline > .Items > li').eq(i).fadeIn();
    $('#Headline > .Pag > ul > li > a').removeClass('Hover');
    $('#Headline > .Pag > ul > li').eq(i).children('a').addClass('Hover');
}

function headlineTrigger()
{
    var len = $('#Headline > .Pag > ul > li').length - 2;
    if(currentItem >= len)
        currentItem = 0;
    else
        currentItem++;
    clearInterval(hlTimer);
    hlTimer = setInterval("headlineTrigger()", 5000);
    headlineTimerMouseOver(currentItem);
}

var currentItemTN = 0;
var imageCountTN = 0;
var hlTimerTN;
function sliderTN() {
    if ($('#TopNews').length > 0) {
        $('#TopNews > ul > li > a:first').addClass('Hover');
        $('#TopNews > .Pag > ul > li').mouseover(function () {
            currentItemTN = $(this).index();
            $('#TopNews > .Pag > ul > li > a').removeClass('Hover');
            $(this).children('a').addClass('Hover');
            $('#TopNews > .Items > li').hide();
            $('#TopNews > .Items > li').eq(currentItemTN).show();
            clearInterval(hlTimerTN);
        }).mouseout(function () {
            hlTimerTN = setInterval("headlineTriggerTN()", 6000);
        });
        hlTimerTN = setInterval("headlineTriggerTN()", 6000);
    }
}

function headlineTimerMouseOverTN (i)
{
    $('#TopNews > .Items > li').fadeOut();
    $('#TopNews > .Items > li').eq(i).fadeIn();
    $('#TopNews > .Pag > ul > li > a').removeClass('Hover');
    $('#TopNews > .Pag > ul > li').eq(i).children('a').addClass('Hover');
}

function headlineTriggerTN()
{
    var len = $('#TopNews > .Pag > ul > li').length - 1;
    if(currentItemTN >= len)
        currentItemTN = 0;
    else
        currentItemTN++;
    clearInterval(hlTimerTN);
    hlTimerTN = setInterval("headlineTriggerTN()", 6000);
    headlineTimerMouseOverTN(currentItemTN);
}

// Ajax yükleme ikonu oluşturucu
function startAjax(elm, ow)
{
    var pos = $(elm).offset();
    if(ow)
    {
        var w = $(elm).outerWidth();
        var h = $(elm).outerHeight();
    }
    else
    {
        var w = $(elm).width();
        var h = $(elm).height();
    }
    var html = '<div id="panel" class="panel"></div><div id="loading" class="loading"></div>';

    $('body').append(html);

    $('#loading, #panel').css({
        top: pos.top,
        left: pos.left,
        width: w,
        height: h,
        opacity: 0.7
    });
    $('#loading').css('opacity', 1);
}

// Ajax yükleme ikonu sonlandırıcı
function endAjax()
{
    $('#loading, #panel').remove();
}